<?php
/*
	诺离函数库（2016年创作，2019年整合到class）
	daozhao_str		倒找文本字符串
	get_ip 		    获取客户IP地址
	to_gbk			将字符串智能转换成gbk编码
	to_utf8 		将字符串智能转换成utf8编码
	getalltext		取所有文本中间
	http             进行一次模拟访问

*/
	class nl {
		//不同环境下获取真实的IP
		function getIp(){
		    //判断服务器是否允许$_SERVER
		    if(isset($_SERVER)){    
		        if(isset($_SERVER[HTTP_X_FORWARDED_FOR])){
		            $realip = $_SERVER[HTTP_X_FORWARDED_FOR];
		        }elseif(isset($_SERVER[HTTP_CLIENT_IP])) {
		            $realip = $_SERVER[HTTP_CLIENT_IP];
		        }else{
		            $realip = $_SERVER[REMOTE_ADDR];
		        }
		    }else{
		        //不允许就使用getenv获取  
		        if(getenv("HTTP_X_FORWARDED_FOR")){
		              $realip = getenv( "HTTP_X_FORWARDED_FOR");
		        }elseif(getenv("HTTP_CLIENT_IP")) {
		              $realip = getenv("HTTP_CLIENT_IP");
		        }else{
		              $realip = getenv("REMOTE_ADDR");
		        }
		    }

		    return $realip;
		}    

		function to_gbk()
	{
		$encode = mb_detect_encoding($keytitle, array("ASCII","UTF-8","GB2312","GBK","BIG5")); 
		if ($encode != "" && $encode!="GBK"){ 
			$keytitle = iconv($encode,"GBK",$keytitle); 
		} 
		return $keytitle;
	}

	function to_utf8($keytitle)
	{
		$encode = mb_detect_encoding($keytitle, array("ASCII","UTF-8","GB2312","GBK","BIG5")); 
		if ($encode != "" && $encode!="UTF-8"){ 
			$keytitle = iconv($encode,"UTF-8",$keytitle); 
		} 
		return $keytitle;
	}


	function daozhao_str($str,$find,$m)
	{
		while(true)
		{
			$findsize = strlen($find);
			$temp_str = substr($str,$m-$findsize,$findsize);
			if($temp_str == $find)
			{
				return $m;
			}
			else
			{
				$m--;
			}
			if($m<0)
			{
				return "";
			}
		}
	}


	function getalltext($data,$p1,$p2,$f_null=false)
	{
		$qs = 0;
		$js = 0;
		$i = 0;
		while(1)
		{
			$qs = strpos($data,$p1,$qs);
			if($qs<=0){break ;}
			$js = strpos($data,$p2,$qs+strlen($p1));
			if($js<=0){break ;}
			$qs += strlen($p1);
			$str = substr($data,$qs,$js-$qs);
			if($str == "")
			{
				if($f_null == true)
				{
				$re[$i] = $str;
				$i++;
				}
			}
			else
			{
				$re[$i] = $str;
				$i++; }
			$qs = $js+strlen($p2);}
		return $re;	
	}

	function http($url, $data="",$cookie = "",$timeout = 30,$headers = "")
	 {
		$cookie_jar = "";
		$r_cookie   = "";
	    $ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE;
	    $ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_HEADER,1);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
		curl_setopt($ch,CURLOPT_COOKIE,$cookie);
		if($headers=="")
		{
		$headers[] = "Accept: */*";
		$headers[] = "Accept-Language: zh-cn";
		$headers[] = "User-Agent: Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; 125LA; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
		$headers[] = "Content-Type: application/x-www-form-urlencoded";
		}
		curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
		if ($ssl){
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FLASE);
		}

		if($data!=""){
		//curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($data));
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		curl_setopt($ch,CURLOPT_POST,1);
		}


		curl_setopt($ch,CURLOPT_COOKIESESSION,true);
		//curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);

		$data = curl_exec($ch);

		$arr1 = explode("Set-Cookie:",$data);
		for($i=1;$i<count($arr1);$i++)
		{
		$arr2 = explode(";",$arr1[$i]);
		if($arr2[0]<>""){
		$r_cookie = $r_cookie . $arr2[0].";";}
		}
		if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
		     $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
		     $header = substr($data, 0, $headerSize);
		     $body = substr($data, $headerSize);
			$info['data'] = $body;
			$info['cookie'] = $r_cookie;
		}
		else
		{
			$info['data'] = $data;
			$info['cookie'] = $r_cookie;
		}

	     curl_close($ch);
	     return $info;
	 }


	 //检查某个值，在数组中是否存在与它
	 function check_value($value,$arrvalue,$keys)
	 {
	 	for($i=0;$i<count($arrvalue);$i++)
	 	{
	 		if($arrvalue[$i][$keys] == $value)
	 		{
	 			return true;
	 		}
	 	}
	 	return false;
	 }


	 function getIpAddr($ip){
		$ret = $this->http("https://ip.taobao.com/outGetIpInfo?ip=$ip&accessKey=alibaba-inc");
		$retJson = json_decode($ret['data'],true);
		return $retJson['data']['region']." ".$retJson['data']['city'];
	 }

	 function fenge2($str ,$fg1 , $fg2)
	{
		$str2 = explode($fg1,$str);
		if(count($str2)>1)
		{
		$str2 = $str2[1];
		}
		else{
		$str2 = "";
		}
		
		$str3 = explode($fg2,$str2);
		return $str3[0];
	}

	 

	}

	

	




?>